/*
 * Base class for drawing SvgHelper
 */

package serveursvg;

import java.awt.Point;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Serializable;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 *
 */
public class SvgHelper implements Serializable {
    String header;
    String footer= "</svg>";
    String content="";
    String ligneDeVie="";

    public String getLigneDeVie() {
        return ligneDeVie;
    }

    public void setLigneDeVie(String ligneDeVie) {
        this.ligneDeVie = ligneDeVie;
    }
    public static final String  BLEUE = "blue";
    public static final String  RED = "red";
    public static final String  GREEN = "green";
    public static final String  BLACK = "black";
    public static final String  YELLOW = "yellow";
    
    public void writeSVGToFile(String fileName){
        try {
            FileWriter filewriter = null;
            File file = new File(fileName);
            filewriter = new FileWriter(file, false);
            filewriter.write(this.toString());
            filewriter.close();
            System.out.println("Svg file dumped to: " + fileName);
        } catch (IOException ex) {
            Logger.getLogger(SvgHelper.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    public void generateSVGFromContent(String content, int width, int height, String fileName){
        try {
            FileWriter filewriter = null;
            File file = new File(fileName);
            filewriter = new FileWriter(file);
            setHeader(width, height);
            setContent(content);
            filewriter.write(this.toString());
            filewriter.close();
        } catch (IOException ex) {
            Logger.getLogger(SvgHelper.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

//    public String tracerFleche(Point x, Point y){
//        String fleche = "<path d=\"M " +x.x + " " + x.y + " L "+ x.x + " " + x.y +" L "+y.x + " " + y.y + "\" \n"
//                + "        style=\"fill:none; stroke:black; stroke-width:1;\n"
//                + "        marker-end:url(#Triangle)\" />";
//        content+=fleche;
//        return fleche;
//    }

    public void tracerFleche(Point x, Point y, String color){
        String fleche = "\n<path d=\"M " +x.x + " " + x.y + " L "+ x.x + " " + x.y +" L "+y.x + " " + y.y + "\" \n"
                + "        style=\"fill:none; stroke:" + color + "; stroke-width:1;\n"
                + "        marker-end:url(#Triangle)\" />";
        content+=fleche;
    }
    public String ligneDeVie(Point x, Point y, String color){
        String fleche = "\n<path d=\"M " +x.x + " " + x.y + " L "+ x.x + " " + x.y +" L "+y.x + " " + y.y + "\" \n"
                + "        style=\"fill:none; stroke:" + color + "; stroke-width:1;\n"
                + "        marker-end:url(#Triangle)\" />";
        return fleche;
    }

    public void tracerRectangle(Point x, Point y, String couleur){
        String rectangle = "\n<rect x=\""+x.x+"\" y=\""+x.y+"\" "
                + "width=\""+new Integer(y.x-x.x)+"\" height=\""+y.y+"\" "+
        "fill=\""+couleur+"\"  stroke-width=\"2\" />";
        content+=rectangle;
//       return rectangle ;
    }

    public void tracerTrait(Point x, Point y, String couleur){
        String trait;
        trait = "\n<line x1=\""+ x.x +"\" y1=\""+x.y+"\" x2=\""+y.x+"\" y2=\""+y.y+"\" "
                + "style=\"stroke:"+couleur+";stroke-width:2\"/>";
        content+=trait;
//        return trait;
    }

    public void ecrireTexte(Point x, String taillePolice, String texte, String couleur){
        String txt;
        txt = "\n<text x=\""+x.x+"\" y=\""+x.y+"\" font-size=\""+taillePolice+"\" fill=\""+couleur+"\">"+texte+"</text>";
        content+=txt;
//        return txt;
    }

    public String getContent() {
        return content;
    }

    public void setContent(String content) {
        this.content = content;
    }

    public String getFooter() {
        return footer;
    }

    public void setFooter(String footer) {
        this.footer = "</svg>";
    }

    public String getHeader() {
        return header;
    }

    public void setHeader(int width, int height) {
        this.header = "<?xml version=\"1.0\" standalone=\"no\"?>\n"
            + "<!DOCTYPE svg PUBLIC \"-//W3C//DTD SVG 20010904//EN\"\n"+
   "\"http://www.w3.org/TR/2001/REC-SVG-20010904/DTD/svg10.dtd\">\n"+
"<svg width=\""+width+"\" height=\""+height+"\"\n"+
     "xmlns=\"http://www.w3.org/2000/svg\">\n";
        String trianglePourFleche="<defs>\n"
                + "    <marker id=\"Triangle\" \n"
                + "      viewBox=\"0 0 10 10\" refX=\"9\" refY=\"5\"\n"
                + "      markerWidth=\"8\" markerHeight=\"8\"\n"
                + "      orient=\"auto\">\n"
                + "      <path d=\"M 0 0 L 10 5 L 0 10 z\" />\n"
                + "    </marker>\n"
                + "  </defs>\n";
        header+=trianglePourFleche;
    }
    @Override
    public String toString(){
        return header + ligneDeVie + content + footer;
    }

//    private void writeObject(java.io.ObjectOutputStream out)
//     throws IOException{
//
//        out.writeObject(header);
//        out.writeObject(content);
//        out.writeObject(header);
//        out.flush();
//
//    }
//    private void readObject(java.io.ObjectInputStream in)
//     throws IOException, ClassNotFoundException{
//        header = (String) in.readObject();
//        content = (String) in.readObject();
//        footer = (String) in.readObject();
//
//
//    }
//    private void readObjectNoData()
//     throws ObjectStreamException{
//
//    }
}
